package com.yao.longhan.ui.fragment;

import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.yao.longhan.BaseApplication;
import com.yao.longhan.R;
import com.yao.longhan.dao.StoryArticleDao;
import com.yao.longhan.eventbus.AddToShelfEvent;
import com.yao.longhan.eventbus.RxEvent;
import com.yao.longhan.model.StoryArticle;
import com.yao.longhan.presenter.adapter.HomeAdapter;
import com.yao.longhan.ui.base.BaseFragment;

import java.util.List;

import butterknife.BindView;

/**
 * Created by heyao on 2017/7/24.
 */

public class HomeFragment extends BaseFragment {

    @BindView(R.id.home_recycler_view)
    RecyclerView mRecyclerView;

    private HomeAdapter mHomeAdapter;

    @Override
    protected int layoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initAfter() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mActivity, 3);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mHomeAdapter = new HomeAdapter(mApp);
        mRecyclerView.setAdapter(mHomeAdapter);
        loadData();
    }

    private void loadData() {
        StoryArticleDao dao = BaseApplication.getInstance().getDaoSession().getStoryArticleDao();
        List<StoryArticle> list = dao.queryBuilder().orderDesc(StoryArticleDao.Properties.ReadCount).list();
        if (list != null) {
            mHomeAdapter.addDataBeforeClean(list);
            mHomeAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onEventMainThread(RxEvent rxEvent) {
        if (rxEvent instanceof AddToShelfEvent) {
            loadData();
        }
    }
}
